export default class Request {
	// 判断url是否为绝对路径
	static absolutelyUrl(url) {
		return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
	};

	// 发起请求前
	static beforeRequest(config) {
		return config
	};

	// 发起请求后
	static afterRequest(response) {
		return response
	};

	// 基本配置
	config = {
		baseUrl: '',
		header: {
			'Content-Type': 'application/json;charset=UTF-8'
		},
		method: 'GET',
		dataType: 'json',
		responseType: 'text',
		success() {},
		fail() {},
		complete() {}
	};

	// 过滤器
	interceptor = {
		beforeRequest(callback) {
			if (callback) {
				Request.beforeRequest = callback
			}
		},
		afterRequest(callback) {
			if (callback) {
				Request.afterRequest = callback
			}
		}
	};

	// 设置配置
	setConfig(callback) {
		this.config = callback(this.config)
	}

	// 发起请求
	request(options = {}) {
		// 基本参数
		options.baseUrl = options.baseUrl || this.config.baseUrl
		options.dataType = options.dataType || this.config.dataType
		options.url = Request.absolutelyUrl(options.url) ? options.url : (options.baseUrl + options.url)
		options.data = options.data || {}
		options.header = options.header || this.config.header
		options.method = options.method || this.config.method

		// promise 请求
		return new Promise((resolve, reject) => {
			let execute = true
			let _config = null
			// 请求完成
			options.complete = (response) => {
				response.config = _config
				return resolve(Request.afterRequest(response))
			}
			// 默认配置 + 用户配置【复制作用】
			let mConfig = {
				...this.config,
				...options
			}
			// 取消请求 - 回调
			let cancel = (msg = 'request cancel') => {
				execute = false
				return resolve([{
					config: mConfig,
					data: {
						code: 0,
						msg: msg
					}
				}])
			}
			// 默认配置 + 用户配置 + 初始配置【复制作用】
			_config = {
				...mConfig,
				...Request.beforeRequest(mConfig, cancel)
			}
			// 是否取消请求【如果execute == false, 那么已经resolve了，所以不需要return resolve】
			if (!execute) return
			
			// 移除addon前缀
			if (_config.hasOwnProperty('removeAddon')) {
				_config.url = _config.url.replace(/\/addons\/[a-z0-9]+/g, '')
			}

			// 发起请求
			if (_config.hasOwnProperty('uploadFile')) {
				const data = _config.data
				let tConfig = {
					url: _config.url,
					filePath: data.file.file,
					name: data.file.name,
					formData: data.form,
					// #ifdef  MP-WEIXIN
					header: _config.header,
					// #endif
					success: _config.success,
					fail: _config.fail,
					complete: _config.complete,
				}
				console.log('uni.uploadFile', tConfig)
				uni.uploadFile(tConfig)
			} else {
				uni.request(_config)
			}
		})
	};

	// 接口请求 - 支持重试
	autoRetry(options, retryMax = 3) {
		console.log('autoRetry', options, retryMax);
		return new Promise((resolve, reject) => {
			if (retryMax <= 0) {
				return resolve(['网络错误']);
			}
			if (retryMax == 2) {
				console.log('this.request exect');
				this.request(options).then(([err, res]) => {
					console.log('this.request', err, res);
					if (!err) {
						resolve([err, res]);
					} else {
						this.autoRetry(options, retryMax - 1);
					}
				});
			} else {
				return this.autoRetry(options, retryMax - 1);
			}
		});
	}

	// GET 请求
	get(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	}

	// POST 请求
	post(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	}
}